/**
  ******************************************************************************
  * File Name          : taskctrl.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "bsp.h"
#include "tskcfg.h"
#include <string.h>
#include "lora.h"
#include "sensor.h"
#include "edct.h"
/*------------ task define ------------*/

TaskHandle_t xRunDog_Thd = NULL;
StaticTask_t xRunDog_Tcb;
StackType_t  xRunDog_Stk[ RUNDOG_STACK_SIZE ];

//char PrintfBuf[200];
/**
  * @brief  Create task
  * @param  None
  * @retval None
  */
void APP_CreateTask(void)
{
	xRunDog_Thd = xTaskCreateStatic(vRunDog_Task,
	                                 "RunD",
	                                 RUNDOG_STACK_SIZE,
	                                 (void *)1,
																	 RUNDOG_TSK_PRIORITY,
																	 xRunDog_Stk,
																	 &xRunDog_Tcb);

	xTerminal_Thd = xTaskCreateStatic(vTerminal_Task,
																	  "term",
																	  TTY_STACK_SIZE,
																	  (void *)1,
																	  TTY_TSK_PRIORITY,
																	  xTerminal_Stk,
																	  &xTerminal_Tcb);

  xStoragePara_Thd = xTaskCreateStatic(vStoragePara_Task,
										                   "para",
																	     STGPARA_STACK_SIZE,
																	     (void *)1,
																	     STGPARA_TSK_PRIORITY,
																	     xStoragePara_Stk,
																	     &xStoragePara_Tcb);
#ifdef EDCT_JUST_PRESS
  xGui_Thd = xTaskCreateStatic(vGui_Task,
                               "gui",
                               GUI_STACK_SIZE,
                               (void *)1,
                               GUI_TSK_PRIORITY,
                               xGui_Stk,
                               &xGui_Tcb);
#endif
	xSensorMain_Thd = xTaskCreateStatic(vSensorMain_Task,
                                      "sema",
                                      SENSORMAIN_STACK_SIZE,
                                      (void *)1,
                                      SENSORMAIN_TSK_PRIORITY,
                                      xSensorMain_Stk,
                                      &xSensorMain_Tcb);
																			 
	xLoRaRecv_Thd = xTaskCreateStatic(vLoRaReceive_Task,
                                    "LrRc",
                                    LORARECV_STACK_SIZE,
                                    (void *)1,
                                    LORARECV_TSK_PRIORITY,
                                    xLoRaRecv_Stk,
                                    &xLoRaRecv_Tcb);
}

void vRunDog_Task( void * pvParameters )
{
  uint8_t step = 0;
                                    
	while(1)
	{
    if(step >= 10)
    {
      LL_IWDG_ReloadCounter(IWDG);
      step = 0;
    }
    
    step++;
    
#ifdef EDCT_JUST_PRESS
    printf("p:%d", SENSOR.pressure);
#else
    if(SHT30_FailCount == 0)
    {
      printf("t:%.2f, h:%.2f", SENSOR.temperature, SENSOR.humidity);
    }
    else if((SHT30_FailCount == 10) || (SHT30_FailCount == 15) || (SHT30_FailCount == 20))
    {
      printf("sht30_soft_reset");
    }
    else if((SHT30_FailCount == 25) || (SHT30_FailCount == 30)|| (SHT30_FailCount == 35))
    {
      printf("sht30_i2c_reset");
    }
    else if((SHT30_FailCount > 35) && (SHT30_FailCount < 61) && ((SHT30_FailCount % 5) == 0))
    {
      printf("sht32_reset_power");
    }
    else
    {
      printf("sht30_error");
    }
    
    if(NH3_FailCount == 0)
    {
      if(NH3_WarmCount > 1)
      {
        printf(", n:%d, nwc:%d", SENSOR.nh3, NH3_WarmCount);
      }
      else
      {
        printf(", n:%d", SENSOR.nh3);
      }
    }
    else if((NH3_FailCount > 0) && (NH3_FailCount < 10))
    {
      printf(", nh3_lost");
    }
    else
    {
      printf(", nh3_error");
    }
    
    if(Edct_RunPara.tvoc_en == 1)
    {
      printf(", v:%d, c:%d", SENSOR.tvoc, SENSOR.co2);
    }
    
    if(Edct_RunPara.sdp810_en == 1)
    {
      printf(", p:%d", SENSOR.pressure);
    }
#endif
    
    printf("\r\n");
    
    if(LoRa_ConnectFlag == 1)
    {
      LED2(LED_On);
      vTaskDelay(750);
      LED2(LED_Off);
      vTaskDelay(250);
    }
    else
    {
      LED2(LED_On);
      vTaskDelay(250);
      LED2(LED_Off);
      vTaskDelay(750);
    }
	}
}

/* GetIdleTaskMemory prototype (linked to static allocation support) */
void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );

static StaticTask_t xIdleTaskTCBBuffer;
static StackType_t xIdleStack[configMINIMAL_STACK_SIZE];

void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
{
  *ppxIdleTaskTCBBuffer = &xIdleTaskTCBBuffer;
  *ppxIdleTaskStackBuffer = &xIdleStack[0];
  *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
}

/* Private application code --------------------------------------------------*/

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
